//
//  HistoryTableViewController.m
//  ICup
//
//  Created by liao Echo on 14-10-12.
//  Copyright (c) 2014年 Echo. All rights reserved.
//

#import "HistoryTableViewController.h"
#import "RecInfo.h"
#import "singlePeripheral.h"

@interface HistoryTableViewController ()

@end

@implementation HistoryTableViewController

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    curArray = [[NSMutableArray alloc] init];
    _sectionIndexTitles = [[NSMutableArray alloc] init];
    curArrayTime = [[NSMutableArray alloc] init];
    NSString *path = [RecInfo storePathWithFileName:ICUPRECINFO];
    if ([[NSFileManager defaultManager] fileExistsAtPath:path]) {
        NSArray *array =  [RecInfo unarchiverObjectFromPath:path key:ICUPRECINFO];
        [curArray addObjectsFromArray:array];
    }
    
    
    dic = [[NSMutableDictionary alloc] init];
    for (NSMutableArray *array in curArray) {
        if (array.count > 4) {
            NSNumber *ml = [array objectAtIndex:0];
            NSNumber *year = [array objectAtIndex:1];
            NSNumber *month = [array objectAtIndex:2];
            NSNumber *day = [array objectAtIndex:3];
            NSString *msg = [NSString stringWithFormat:@"%@-%@-%@", year, month, day];
            
            if ([dic objectForKey:msg]) {
                NSNumber *preMl = [dic objectForKey:msg];
                ml = [NSNumber numberWithInteger:[preMl integerValue]+[ml integerValue]];
            }
            [dic setObject:ml forKey:msg];
        }
    }
    
    NSArray *nsArry = [[dic allKeys] sortedArrayUsingSelector:@selector(compare:)];
    for (NSString *time in nsArry) {
//        [_sectionIndexTitles insertObject:time atIndex:0];
        [_sectionIndexTitles addObject:time];
    }

    
    
    
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 57;
}
//-(CGFloat)tableView:(UITableView *)aTableView heightForHeaderInSection:(NSInteger)section {
//    return 20;
//}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return  _sectionIndexTitles.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableview cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *CellIdentifier = @"SettingBaseCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    cell.backgroundColor = [UIColor clearColor];//ios7必须要这样处理，不然默认会是白色
    
//    NSMutableArray *array = [curArray objectAtIndex:indexPath.row];
    
    
    
    cell.textLabel.font = [UIFont systemFontOfSize:16];
    cell.textLabel.numberOfLines = 0;
    cell.textLabel.textColor = [UIColor grayColor];
    
//    if (array.count > 4) {
//        
//        NSNumber *year = [array objectAtIndex:1];
//        NSNumber *month = [array objectAtIndex:2];
//        NSNumber *day = [array objectAtIndex:3];
//        
////        cell.textLabel.text = @"2014-10-01";
//        
//        cell.textLabel.text = [NSString stringWithFormat:@"%@-%@-%@", year, month, day];
//    }
    cell.textLabel.text = [_sectionIndexTitles objectAtIndex:indexPath.row];
    
    
    for (int i=0; i<2; i++) {
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(100+i*100, 0, 100, 30)];
        label.textAlignment = UITextAlignmentCenter;
        label.textColor = [UIColor blackColor];
        label.font = [UIFont systemFontOfSize:17];
        label.backgroundColor = [UIColor clearColor];
        [cell addSubview:label];
        if (i == 0) {
            label.text = @"日饮水量";
        }else{
            label.text = @"分";
        }
    }
    for (int i=0; i<2; i++) {
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(100+i*100, 30, 100, 20)];
        label.textAlignment = UITextAlignmentCenter;
        label.textColor = RGB(64, 64, 64, 1);
        label.font = [UIFont systemFontOfSize:15];
        label.backgroundColor = [UIColor clearColor];
        [cell addSubview:label];
        
//        label.text = [[dic allValues] objectAtIndex:indexPath.row];
        if (i == 0) {
            label.text = @"107ml";
//            if (array.count > 1){
//                NSNumber *ml = [array objectAtIndex:0];
                label.text = [NSString stringWithFormat:@"%dml", [[dic objectForKey:[_sectionIndexTitles objectAtIndex:indexPath.row]] integerValue]*5];
//            }
        }else{
//            if (array.count > 1){
//                NSNumber *ml = [array objectAtIndex:0];
                label.text = [NSString stringWithFormat:@"%d 分", [singlePeripheral getSroce:[[dic objectForKey:[_sectionIndexTitles objectAtIndex:indexPath.row]] intValue]]];
//            }
        }
    }
    
    
    return cell;
}
@end
